﻿using System;
using System.Data;
using System.IO;
using Distribution.Data;
using Distribution.General;
using Entities;

namespace Distribution.Service
{
    public class LPole
    {
        public static DataTable GetPoleDetails()
        {
            return DPole.GetPoleDetails();
        }

        public static void Delete(EPole pole)
        {
            DPole.Delete(pole);
        }

        public static void Save(EPole pole)
        {
            DPole.Save(pole);

            foreach (EImage i in pole.Images)
            {
                FileInfo _file = new FileInfo(PPIConstants.K_ImagesFolder + i.TempFileName);

                if (_file.Exists && !i.IsDeleted && i.Id == Int32.MinValue)
                    i.FileName = _file.CopyTo(PPIConstants.K_ImagesFolder + i.FileName, true).Name;
            }
        }

        public static void Update(EPole pole)
        {
            DPole.Update(pole);

            foreach (EImage i in pole.Images)
            {
                if (!i.FileName.Equals(i.TempFileName)) 
                {
                    FileInfo _file = new FileInfo(PPIConstants.K_ImagesFolder + i.TempFileName);

                    if (_file.Exists && !i.IsDeleted && i.Id == Int32.MinValue)
                    i.FileName = _file.CopyTo(PPIConstants.K_ImagesFolder + i.FileName, true).Name;
                }
            }
        }


        public static EPole GetById(EPole pole)
        {
            return DPole.GetById(pole);
        }

        public static EPole GetBasicDataById(EPole pole)
        {
            return DPole.GetBasicDataById(pole);
        }
    }
}
